package com.example.javalib.java;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Description:
 * @author: zcs
 * @email: 1059737741@qq.com
 * create at 2022/2/14 16:27
 */
public class FileTest {
    public static void main(String[] args) throws IOException {
        File file = new File("D:/test");
        file.mkdir();// 在D盘下创建test目录
        File[] fileArr = file.listFiles(); // 获取目录下的所有文件
        for (File f : fileArr) { // 输出目录下的文件名
            System.out.println(f.getName());
        }

//        String str;
//        System.out.println("请输入需要复制的文件名：");
//        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
//        str = buf.readLine();
//        if (str.equals("1.txt")) {
           copyTxt();
//        }
//        if(str.equals("1.jpg")){
           copyJpg();
//        }


    }

    //复制 text
    public static void copyTxt() throws IOException {
        File file1 = new File("D:/test/1.txt");
        File file2 = new File("D:/test/2.txt");

        BufferedReader bfRead = new BufferedReader(new FileReader(file1));
        BufferedWriter bfWrite = new BufferedWriter(new FileWriter(file2));

        char[] arry = new char[1024];
        int number = 0;
        while ((number = bfRead.read(arry)) != -1) {
            System.out.println("number:"+ number);
            bfWrite.write(arry, 0, number);
        }
        bfWrite.close();
        bfRead.close();
    }

    //复制 file
    public static void copyJpg() throws IOException  {
        File file1= new File("D:/test/1.png");
        File file2= new File("D:/test/2.png");

        FileInputStream bfRead = new FileInputStream(file1);
        FileOutputStream bfWrite = new FileOutputStream(file2);
        try {
            byte[] arry = new byte[1024]; // 定义数组，传递流之间的数据
            int count = 0;
            while((count = bfRead.read(arry)) != -1) {
                bfWrite.write(arry, 0, count);
            }

        } catch (FileNotFoundException e) {
            System.out.println("filenotfound");
        } catch (IOException e) {
            System.out.println("IoException");
        }
        bfWrite.close();
        bfRead.close();
    }



}
